# -*- coding: utf-8 -*-
# Data structure
#   map[name] = [[PNAME, PTIME], ]
# 
import sys
from xlrd import open_workbook

from config import *

NAME_INDEX=0
P1NAME_INDEX=1
P1TIME_INDEX=2
P2NAME_INDEX=3
P2TIME_INDEX=4

PNAME_INDEX=0
PTIME_INDEX=1
PPERC_INDEX=2

def format_worktime(p1name, p1time, p2name, p2time):
    wt = []
    if (p2name == ""):
        wt = [[p1name, p1time, 100]]
    else:
        try:
            total = p1time + p2time
        except TypeError:
            #print "p1", p1name, p1time, "p2", p2time
            total = p1time
        if total < 5: total = 10
        total /= 100
        p1 = round(p1time/total)
        p2 = 100 - p1
        wt = [[p1name, p1time, p1], [p2name, p2time, p2]]
    return wt

def format_project(p1name):
    if len(p1name) == 0:
        return p1name
    else:
        return p1name.split()[-1]

def read_worktime(filename):
    ret = {}
    wb = open_workbook(filename)   
      
    for s in wb.sheets():   
        for row in range(1, s.nrows):
            name   = s.cell(row, NAME_INDEX  ).value.strip().replace(' ', '')
            p1name = s.cell(row, P1NAME_INDEX).value.strip()
            p2name = s.cell(row, P2NAME_INDEX).value.strip()
            p1time = s.cell(row, P1TIME_INDEX).value
            p2time = s.cell(row, P2TIME_INDEX).value
            #print name, p1name, p1time, p2name, p2time
            ret[name] = format_worktime(format_project(p1name), p1time, format_project(p2name), p2time)
    return ret

def print_xhh_format(name, result, l=30):
    temp = ['0']*l
    dtemp = ['0']*l
    for k, v in result.items():
        temp[k] = str(v)
        if (k>2):
            dtemp[k] = u'开发'.encode('gbk')
    print name+'\t'+'\t'.join(temp)
    print name+'\t'+'\t'.join(dtemp)

def print_matrix(src, nlist=[], plist=[]):
    if (len(plist) == 0):
        for k, v in ret.items():
            print k,
            for wt in v:
                print wt[PNAME_INDEX], wt[PPERC_INDEX],
            print
    else:
        l = len(plist)
        for name in nlist:
            try:
                worktime = src[name]
                #print name.encode('gbk'), 
                result = {}
                for wt in worktime:
                    pname = wt[PNAME_INDEX]
                    try:
                        idx = plist.index(pname)
                    except ValueError:
                        idx = 1
                    result[idx] = wt[PPERC_INDEX]/100
                    #if (pname in plist):
                    #    print 'Y'
                    #else:
                    #    print pname
                #print result
                print_xhh_format(name.encode('gbk'), result, l)
            except KeyError:
                print name.encode('gbk')+ "\tNO"
                print name.encode('gbk')+ "\tNO"

if __name__ == '__main__':
    if len(sys.argv) > 1:
        cmd = sys.argv[1]
        if cmd == 'list':
            print filter_list
        elif cmd == 'test':
            filename = sys.argv[2]
            ret = read_worktime(filename)
            #print_matrix(ret)
            print_matrix(ret, name_list, filter_list)
        else:
            ret = read_worktime(cmd)
            print_matrix(ret)

